import axios from 'axios';
class HttpRequest {
    getConfig() {
        return {
            baseURL: `${process.env.VUE_APP_NODEAPI}`,
            header: {},
            timeout: 5000
        }
    }
    interceptors(instance) {
        instance.interceptors.request.use(
            config => {
                return config;
            },
            error => {
                console.log(error);
                return Promise.reject();
            }
        );
        
        instance.interceptors.response.use(
            response => {
                let config = response.config;            
                console.log(config);
                let method = config["method"];
                let path = config["url"];
                let data = response.data;
                let formNo = '';
                if(data && data.length && data.length > 0){
                    let first = data[0]
                    for(let key in first) {
                        if(key.indexOf('No') > -1 || key.indexOf("Code") > -1) {
                            formNo = first[key];
                            break;
                        }
                    }
                }
                let logContent = createLog(path, method, formNo);
                bus.$emit('addLog', logContent);
                if (response.status === 200) {
                    return response.data;
                } else {
                    Promise.reject();
                }
            },
            error => {
                console.log(error);
                return Promise.reject();
            }
        );
    }
    request(options) {
        const instance = axios.create()
        options = {...this.getConfig(), ...options}
        this.interceptors(instance)
        return instance(options);
    }
}

export default new HttpRequest()
